/**
 * 
 */
package cn.lzh.common.i18n;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * 
 * @author <a href="mailto:sealinglip@gmail.com">Sealinglip</a>
 * 2006-11-7
 */
public class DefaultMessageResources implements IMessageResources {
	private static DefaultMessageResources defaultInstance = null;
	private Properties bundleProps;

    private boolean escape = true;
    
    protected String escape(String string) {
        if (!isEscape()) {
            return string;
        }

        if ((string == null) || (string.indexOf('\'') < 0)) {
            return string;
        }

        int n = string.length();
        StringBuffer sb = new StringBuffer(n);

        for (int i = 0; i < n; i++) {
            char ch = string.charAt(i);

            if (ch == '\'') {
                sb.append('\'');
            }

            sb.append(ch);
        }

        return sb.toString();
    }
    
    protected String formatString(Locale locale, String formatString, Object... args){
        if(formatString == null || args == null || args.length == 0) {
        	return formatString;
        }
        MessageFormat format = new MessageFormat(escape(formatString));
        format.setLocale(locale);
        return format.format(args);
    }
    
	public Properties getBundleProps() {
		return bundleProps;
	}

	/* (non-Javadoc)
	 * @see snt.bizteller.util.IMessageResources#getMessage(java.util.Locale, java.lang.String, java.lang.Object[])
	 */
	public String getMessage(Locale locale, String key, Object... args) {
		return formatString(locale, getResourceBundle("", locale).getString(key), args);
	}

	/* (non-Javadoc)
	 * @see snt.bizteller.util.IMessageResources#getMessage(java.lang.String, java.util.Locale, java.lang.String, java.lang.Object[])
	 */
	public String getMessage(String bundle, Locale locale, String key,
			Object... args) {
		return formatString(locale, getResourceBundle(bundle, locale).getString(key), args);
	}

	/* (non-Javadoc)
	 * @see snt.bizteller.util.IMessageResources#getMessage(java.lang.String, java.lang.Object[])
	 */
	public String getMessage(String key, Object... args) {
		return formatString(Locale.getDefault(), getResourceBundle("", Locale.getDefault()).getString(key), args);
	}

	/* (non-Javadoc)
	 * @see snt.bizteller.util.IMessageResources#getMessage(java.lang.String, java.lang.String, java.lang.Object[])
	 */
	public String getMessage(String bundle, String key, Object... args) {
		return formatString(Locale.getDefault(), getResourceBundle(bundle, Locale.getDefault()).getString(key), args);
	}

	private ResourceBundle getResourceBundle(String bundle, Locale locale){
		String baseName = bundleProps.getProperty(bundle, bundle);
		return ResourceBundle.getBundle(baseName, locale);
	}

	public boolean isEscape() {
        return escape;
    }
	
	public void setBundleProps(Properties bundleProps) {
		this.bundleProps = bundleProps;
	}
	
	public void setEscape(boolean escape) {
        this.escape = escape;
    }
	
	public DefaultMessageResources(){
		if (defaultInstance == null) {
			defaultInstance = this;
		}
	}
	
	public static DefaultMessageResources getInstance(){
		if (defaultInstance == null) {
			new DefaultMessageResources();
		}
		return defaultInstance;
	}
}
